package org.rsbot;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;

import org.rsbot.bot.Bot;
import org.rsbot.gui.BotGUI;
import org.rsbot.log.LogOutputStream;
import org.rsbot.log.SystemConsoleHandler;
import org.rsbot.util.SplashScreen;
import org.rsbot.util.UpdateUtil;

public class Application {

    public static String os = System.getProperty("os.name").toLowerCase();
    private static BotGUI gui;

    public static void main(final String[] args) throws Exception {
        if (os.contains("windows")) {
            final UpdateUtil updater = new UpdateUtil(gui);
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                SplashScreen x = new SplashScreen();
                Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
                /*horizontally*/
                int X = (screen.width / 2) - (x.getWidth() / 2); /*Center*/
                /*vertically*/
                int Y = (screen.height / 2) - (x.getHeight() / 2); /*Center*/

                x.setBounds(X, Y, x.getWidth(), x.getHeight());

                x.setVisible(true);
                updater.checkUpdate(true);
                bootstrap();
                new ScriptExtract(args).run();
                Thread.sleep(100);
                gui = new BotGUI();
                gui.setVisible(true);
                gui.addBot();
                x.setVisible(false);
                x.dispose();
            } catch (final Exception e) {
            }
        } else if (os.contains("mac")) {
            final UpdateUtil updater = new UpdateUtil(gui);
            updater.checkUpdate(true);
            bootstrap();
            new ScriptExtract(args).start();
            gui = new BotGUI();
            gui.setVisible(true);
            gui.addBot();
        } else if (os.contains("linux")) {
            final UpdateUtil updater = new UpdateUtil(gui);
            updater.checkUpdate(true);
            bootstrap();
            new ScriptExtract(args).start();
            gui = new BotGUI();
            gui.setVisible(true);
            gui.addBot();
        }
    }

    /**
     * Returns the Bot for any object loaded
     * in its client. For internal use only
     * (not useful for script writers).
     *
     * @param o Any object from within the client.
     * @return The Bot for the client.
     */
    public static Bot getBot(Object o) {
        return gui.getBot(o);
    }

    /**
     * Returns the size of the panel that
     * clients should be drawn into. For
     * internal use.
     *
     * @return The client panel size.
     */
    public static Dimension getPanelSize() {
        return gui.getPanel().getSize();
    }

    private static void bootstrap() {
        Logger.getLogger("").addHandler(new SystemConsoleHandler());
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

            public void uncaughtException(final Thread t, final Throwable e) {
                Logger.getLogger("EXCEPTION").logp(Level.SEVERE, "EXCEPTION", "", "Unhandled exception in thread " + t.getName() + ": ", e);
            }
        });

        System.setErr(new PrintStream(new LogOutputStream(Logger.getLogger("STDERR"), Level.SEVERE), true));
    }
}
